/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kkks.spring_rest.tick.utils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;

/**
 * Gson帮助类
 * 
 */
public class GsonHelper {
    private static Gson gson = new Gson();

    public static String getJsonString(Object obj) {
        try {
            return gson.toJson(obj);
        } catch (Exception e) {
            LogHelper.logException(e.getMessage());
            e.printStackTrace();
            return "err";
        }
    }

    public static String getJsonString(Object obj, Type typeofSrc) {
        try {
            return gson.toJson(obj, typeofSrc);
        } catch (Exception e) {
            LogHelper.logException(e.getMessage());
            e.printStackTrace(); 
            return "err";
        }
    }

    @SuppressWarnings("rawtypes")
	public static Object getObjectbyJsonString(String jsonStr, TypeToken tt) {
        try {
            return gson.fromJson(jsonStr, tt.getType());
        } catch (Exception e) {
            LogHelper.logException(e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    public static Object getObjectbyJsonString(String jsonStr, Type srcType) {
        try {
            return gson.fromJson(jsonStr, srcType);
        } catch (Exception e) {
            LogHelper.logException(e.getMessage());
            e.printStackTrace();
            return null;
        }
    }
}
